// Copyright 2011 MPT Trading, all rights reserved.
package com.example.facebookexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

/**
 * TODO(pjohnson): Insert description
 * 
 * @author Pablo Johnson (pjohnson@avantica.net)
 */
public class MainFragment extends Fragment {

  private static final String TAG = "MainFragment";
  private UiLifecycleHelper uiHelper;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_main, container, false);
//    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
//    authButton.setFragment(this);
//    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
//    uiHelper = new UiLifecycleHelper(getActivity(), callback);
//    uiHelper.onCreate(savedInstanceState);
    return view;
  }

  private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
      onSessionStateChange(session, state, exception);
    }
  };

  private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
      Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
      Log.i(TAG, "Logged out...");
    }
  }
  
  @Override
  public void onResume() {
      super.onResume();
      Session session = Session.getActiveSession();
      if (session != null &&
             (session.isOpened() || session.isClosed()) ) {
          onSessionStateChange(session, session.getState(), null);
      }

      uiHelper.onResume();
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      uiHelper.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public void onPause() {
      super.onPause();
      uiHelper.onPause();
  }

  @Override
  public void onDestroy() {
      super.onDestroy();
      uiHelper.onDestroy();
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      uiHelper.onSaveInstanceState(outState);
  }
}
